﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMS.Resources;
using System.Collections;
using PMS.Entity;

namespace PMS.Utils
{
    public class BindDataUtil
    {
        /// <summary>
        /// string strResourceKey = ResourceKey; (Key of Resource on Resource)
        /// 
        /// 
        /// //This is Enum Data
        ///     public enum NameTest
        ///     { 
        ///      A1 = 0,
        ///      A2 = 1,
        ///      A3 = 2,
        ///      A4 = 4,
        ///     }
        ///     
        /// //This is Code binding data to ArrayList when have Resource and Enum
        /// Type enumType = typeof(NameTest);
        /// ArrayList data = BindDataUtil.BindDataCombobox(strResourceKey, enumType);
        /// 
        /// //Load Data To combobox
        /// cboTest.Properties.Items.AddRange(data);
        /// cboTest.DisplayMember = "Value";
        /// cboTest.ValueMember = "Key";
        /// </summary>
        /// <param name="resourceString">String from resource files</param>
        /// <returns></returns>
        public static ArrayList BindDataCombobox(string resource, Type enumType)
        {
            var dataEnum = Enum.GetValues(enumType);
            List<KeyValuePair<string, string>> dataResource = BindCombobox(FormResources.GetString(resource));
            ComboItem temp;
            ArrayList result = new ArrayList(); ;

            foreach (KeyValuePair<string, string> itemValue in dataResource)
            {
                foreach (object itemKey in dataEnum)
                {
                    if (string.Format("{0}", itemKey) == itemValue.Key.Trim())
                    {
                        temp = new ComboItem();
                        temp.DisplayValue = itemValue.Value.Trim();
                        temp.ItemValue = Convert.ToInt32(itemKey);
                        result.Add(temp);
                    }
                }
            }

            return result;
        }
        public static List<ComboItem> BindDataCombo(string resource, Type enumType)
        {
            var dataEnum = Enum.GetValues(enumType);
            List<KeyValuePair<string, string>> dataResource = BindCombobox(FormResources.GetString(resource));
            ComboItem temp;
            List<ComboItem> result = new List<ComboItem>(); ;

            foreach (KeyValuePair<string, string> itemValue in dataResource)
            {
                foreach (object itemKey in dataEnum)
                {
                    if (string.Format("{0}", itemKey) == itemValue.Key.Trim())
                    {
                        temp = new ComboItem();
                        temp.DisplayValue = itemValue.Value.Trim();
                        temp.ItemValue = Convert.ToInt32(itemKey);
                        result.Add(temp);
                    }
                }
            }

            return result;
        }
        private static List<KeyValuePair<string, string>> BindCombobox(string resourceString)
        {
            string pattern = resourceString;
            string[] composite = resourceString.Split('#');
            List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();

            foreach (string pairValue in composite)
            {
                string[] pair = pairValue.Split('=');

                if (pair.Length > 1)
                {
                    data.Add(new KeyValuePair<string, string>(pair[0].ToString(), pair[1].ToString()));
                }
                else
                {
                    data.Add(new KeyValuePair<string, string>(pairValue, pairValue));
                }
            }

            return data;
        }

        //public static ArrayList SetComboItemsFromListObject<T>(List<T> list, string itemvalue, string displayvalue)
        //{
        //    ArrayList result = new ArrayList();

        //    foreach (var item in list)
        //    {
        //        ComboItemExp temp = new ComboItemExp();
        //        temp.DisplayValue = item.GetType().GetProperty(displayvalue).GetValue(item, temp.DisplayValue);
        //        temp.ItemValue = typeof(T).GetField(itemvalue).GetValue(item);
        //        result.Add(temp);
        //    }

        //    return result;
        //}
    }
}
